use tauri::api::shell::open;
use tauri::Manager;

use crate::runtime::error::MagicBoxError;
use crate::runtime::response::{Code, MagicBoxResponse};
use crate::runtime::result::MagicBoxResult;
use crate::state;

#[tauri::command(rename_all = "snake_case")]
pub(crate) async fn invoke_open(path: String) -> MagicBoxResult<MagicBoxResponse<()>> {
    let scope = state::application::app().shell_scope();
    let result = open(&scope, path.as_str(), None);
    if result.is_err() {
        return Err(MagicBoxError::new(Code::UndefinedError, result.err().unwrap().to_string().as_str()));
    }

    Ok(MagicBoxResponse::new_empty_success())
}